<?php

/**
 * Easykiosk - class.Compte Client.php
 *
 * Fichier chargé de gérer le compte du client.
 * Comprend son authentification.
 *
 */


class CompteClient
{
	 
	/**
	 * Short description of attribute IDClient
	 *
	 * @access public
	 * @var Integer
	 */
	public $IDClient = null;

	/**
	 * Short description of attribute Nom
	 *
	 * @access public
	 * @var String
	 */
	public $Nom = null;

	/**
	 * Short description of attribute Prenom
	 *
	 * @access public
	 * @var String
	 */
	public $Prenom = null;

	/**
	 * Short description of attribute AdresseMail
	 *
	 * @access public
	 * @var String
	 */
	public $AdresseMail = null;

	protected $credit;
	protected $idClasseur;
	/**
	 * Short description of attribute Password
	 *
	 * @access public
	 * @var String
	 */
	public $Password = null;
	
	public $CleActivation = null;

	// --- OPERATIONS ---

	/**
	 * Crée un nouveau client
	 * @param $Nom
	 * @param $Prenom
	 * @param $AdresseMail
	 * @param $DateNaissance
	 * @param $Password
	 * @return unknown_type
	 */
	public function __construct($Nom,$Prenom,$AdresseMail,$Password=null){
		 
		$this->Nom = $Nom;
		$this->Prenom = $Prenom;
		$this->AdresseMail = $AdresseMail;
		$this->Password = $Password;
		 
		 
	}

	public function getIdClient(){ return $this->IDClient; }
	public function getNom(){	return $this->Nom; }
	public function getPrenom(){ return $this->Prenom; }
	public function getMail(){	return $this->AdresseMail; }
	public function getCredit() { return $this->credit; }
	public function getIdClasseur() { return $this->idClasseur; }
	
	public function setCredit($credit) { $this->credit = $credit; }
	public function setIdClasseur($id) { $this->idClasseur = $id; }
	public function setIdClient($id){	$this->IDClient = $id; }
	
	public function setActivationKey($CleActivation){
		$this->CleActivation = $CleActivation;
	}
	
	public static function authentifie($login, $password){
		$tbh = ConnexionBD::getInstance();
		$stat = $tbh->prepare("SELECT id_compte_client, nom_client, prenom_client,
			mail_client, id_classeur, credit FROM compte_client 
			WHERE mail_client=:mail AND password=:password AND cle_activation IS NULL ");
		$stat->execute(array(':mail' => $login,
							':password' => md5($password))
					  );
		
		if ($data = $stat->fetch()){
			$cptcl = new CompteClient($data['nom_client'],$data['prenom_client'],$data['mail_client']);
			$cptcl->setCredit($data['credit']);
			$cptcl->setIdClasseur($data['id_classeur']);
			$cptcl->setIdClient($data['id_compte_client']);
			return $cptcl;
		}
		else
			return null;
	}
	
	public function export(){
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		$stat = $tbh->prepare("INSERT INTO compte_client SET nom_client=:nom_client,
			prenom_client=:prenom_client, mail_client =:mail_client, password=:password,
			credit=0,id_classeur=0,cle_activation=:cle_activation");
		$array = array(':nom_client'		=> $this->Nom,
							 ':prenom_client'	=> $this->Prenom,
							 ':mail_client'		=> $this->AdresseMail,
							 ':password'		=> md5($this->Password),
							 ':cle_activation'	=> $this->CleActivation);
		$stat->execute($array);
		$this->IDClient = $tbh->lastInsertId();
	}


	public function recharger($credit){
		$credit = str_replace(",",".",$credit);
		if ($credit < 15){
			$_SESSION['message'] = "Veuillez recharger pour un montant supérieur à 15&euro;";
			return;
		}
		
		$tbh = ConnexionBD::getInstance();
		$stat = $tbh->prepare("UPDATE compte_client SET credit = credit + :credit WHERE 
			id_compte_client=:id_compte_client ");
		$array = array(':credit'		=> $credit,
							 ':id_compte_client'	=> $this->IDClient);
		$stat->execute($array);
		
		$this->credit += $credit;
	}

	public function debiter($debit)
	{
		$debit = str_replace(",",".",$debit);
		$tbh = ConnexionBD::getInstance();
		$stat = $tbh->prepare("UPDATE compte_client SET credit = credit - :debit WHERE 
			id_compte_client=:id_compte_client ");
		$array = array(':debit'		=> $debit,
							 ':id_compte_client'	=> $this->IDClient);
		$stat->execute($array);
		
		$this->credit -= $debit;
	}
	
	public function ajouterClasseur(Panier $panier)
	{
		$i = 0;
		$tbh = ConnexionBD::getInstance();
		while ($i<$panier->getNombresArticles())
		{
			$id_client = $this->getIdClient();
			$edition = $panier->getObjetPanier($i);
			$id_edition = $edition->getIDEdition();
			$url = $edition->getURL().".pdf";
			$stat = $tbh->prepare("INSERT INTO exemplaire SET id_client=:id_client,
			id_edition = :id_edition,URL = :url");
			$array = array(':id_client'		=> $id_client,
							 ':id_edition'	=> $id_edition,
							 ':url'		=> $url);
			$stat->execute($array);
			$i++;
		}
		$tpl = new Template('tpl'); // creation du template $template
	 	$tpl->set_file(array( 
		"confirmation"=>"commande_effectuee.tpl" 
		));
		$tpl->set_var("DEBIT", $_GET['debit']);
		
		$tpl->parse("CONFIRMATION","confirmation");
		
		return $tpl->get("CONFIRMATION"); 		
	}
	
}

 /* end of class Compte Client */

?>